from wx import wx
import wx.richtext
import sqlite3

from StringIO import StringIO

# Create Content Class
class CreateContentTab(wx.Panel):
    
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self.toolbar_ADDCONTENT = wx.NewId()
        self.toolbar_UPDATECONTENT = wx.NewId()
        # toolbar_STRIKETHROUGH = wx.NewId()
        # toolbar_SUBSCRIPT = wx.NewId()
        # toolbar_SUPERSCRIPT = wx.NewId()
        # toolbar_ORDEREDLIST = wx.NewId()
        # toolbar_UNORDEREDLIST = wx.NewId()
        richToolbar = self.MakeToolBar()
        self.box = wx.BoxSizer(wx.VERTICAL)
        self.createContentRichTextCtrl = wx.richtext.RichTextCtrl(self, -1, style=wx.VSCROLL | wx.BORDER_SUNKEN | wx.ALWAYS_SHOW_SB | wx.WANTS_CHARS)
        self.listBox = wx.ListBox(self, -1, style= wx.BORDER_SUNKEN | wx.LB_SINGLE | wx.LB_ALWAYS_SB | wx.LB_HSCROLL)
        self.box.Add(richToolbar, 0, wx.ALL | wx.ALIGN_LEFT | wx.EXPAND)
        self.box.Add(self.createContentRichTextCtrl, 1, wx.EXPAND)
        self.box.Add(self.listBox, 1, wx.EXPAND)
        self.SetSizer(self.box)
        wx.EVT_LISTBOX(self, self.listBox.GetId(), self.OnSelectedContentItem)
        
    # Create Event Function
    def OnSelectedContentItem(self, event):
        if(self.listBox.GetSelection() != -1):
            tmpString = self.listBox.GetStringSelection()
            tmpString2 = tmpString.split(".", 1)
            self.content = self.TopLevelParent.currentContentDict[int(tmpString2[0])]
            self.content = self.content[0].encode("UTF-8")
            out = StringIO()
            handler = wx.richtext.RichTextXMLHandler()
            txtBuffer = self.createContentRichTextCtrl.GetBuffer()
            txtBuffer.AddHandler(handler)
            out.write(self.content)
            out.seek(0)
            handler.LoadStream(txtBuffer, out)
            out.close()
            self.createContentRichTextCtrl.Refresh()

    # Create RichText Toolbar
    def MakeToolBar(self):
        tb = wx.ToolBar(self, -1, style=wx.TB_FLAT | wx.NO_BORDER)
        self.ToolBar = tb
        tb.AddTool(wx.ID_CUT, wx.Bitmap("images/edit-cut.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Cut")
        tb.AddTool(wx.ID_COPY, wx.Bitmap("images/edit-copy.png", wx.BITMAP_TYPE_PNG), isToggle=False,shortHelpString="Copy")
        tb.AddTool(wx.ID_PASTE, wx.Bitmap("images/edit-paste.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Paste")
        tb.AddTool(wx.ID_UNDO, wx.Bitmap("images/edit-undo.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Undo")
        tb.AddTool(wx.ID_REDO, wx.Bitmap("images/edit-redo.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Redo")
        tb.AddSeparator()
        tb.AddTool(wx.ID_BOLD, wx.Bitmap("images/format-text-bold.png", wx.BITMAP_TYPE_PNG), wx.Bitmap("images/format-text-bold-off.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Bold")
        tb.AddTool(wx.ID_ITALIC, wx.Bitmap("images/format-text-italic.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Italic")
        tb.AddTool(wx.ID_UNDERLINE, wx.Bitmap("images/format-text-underline.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Underline")
        # tb.AddTool(toolbar_STRIKETHROUGH, wx.Bitmap("images/format-text-strikethrough.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Strikethrough")
        # tb.AddTool(toolbar_SUBSCRIPT, wx.Bitmap("images/Subscript.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Subscript")
        # tb.AddTool(toolbar_SUPERSCRIPT, wx.Bitmap("images/Superscript.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Superscript")
        tb.AddSeparator()
        tb.AddTool(wx.ID_JUSTIFY_LEFT, wx.Bitmap("images/format-justify-left.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Left Align")
        tb.AddTool(wx.ID_JUSTIFY_CENTER, wx.Bitmap("images/format-justify-center.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Align Center")
        tb.AddTool(wx.ID_JUSTIFY_RIGHT, wx.Bitmap("images/format-justify-right.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Right Align")
        tb.AddTool(wx.ID_JUSTIFY_FILL, wx.Bitmap("images/format-justify-fill.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Justify")
        tb.AddSeparator()
        # tb.AddTool(toolbar_ORDEREDLIST, wx.Bitmap("images/NumbersList.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Ordered List")
        # tb.AddTool(toolbar_UNORDEREDLIST, wx.Bitmap("images/BulletList.png", wx.BITMAP_TYPE_PNG), isToggle=True, shortHelpString="Unordered List")
        # tb.AddSeparator()
        # tb.AddTool(wx.ID_INDENT, wx.Bitmap("images/format-indent-more.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Indent")
        # tb.AddTool(wx.ID_UNINDENT, wx.Bitmap("images/format-indent-less.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Outdent")
        # tb.AddSeparator()
        tb.AddTool(self.toolbar_ADDCONTENT, wx.Bitmap("images/list-add.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Add New Content")
        tb.AddTool(self.toolbar_UPDATECONTENT, wx.Bitmap("images/document-save.png", wx.BITMAP_TYPE_PNG), isToggle=False, shortHelpString="Update Existing Content")
        wx.EVT_TOOL(self, wx.ID_CUT, self.ForwardEvent)
        wx.EVT_TOOL(self, wx.ID_COPY, self.ForwardEvent)
        wx.EVT_TOOL(self, wx.ID_PASTE, self.ForwardEvent)
        wx.EVT_TOOL(self, wx.ID_UNDO, self.ForwardEvent)
        wx.EVT_TOOL(self, wx.ID_REDO, self.ForwardEvent)
        wx.EVT_TOOL(self, wx.ID_BOLD, self.OnBold)
        wx.EVT_UPDATE_UI(self, wx.ID_BOLD, self.OnUpdateBold)
        wx.EVT_TOOL(self, wx.ID_ITALIC, self.OnItalic)
        wx.EVT_UPDATE_UI(self, wx.ID_ITALIC, self.OnUpdateItalic)
        wx.EVT_TOOL(self, wx.ID_UNDERLINE, self.OnUnderline)
        wx.EVT_UPDATE_UI(self, wx.ID_UNDERLINE, self.OnUpdateUnderline)
        wx.EVT_TOOL(self, wx.ID_JUSTIFY_LEFT, self.OnAlignLeft)
        wx.EVT_UPDATE_UI(self, wx.ID_JUSTIFY_LEFT, self.OnUpdateAlignLeft)
        wx.EVT_TOOL(self, wx.ID_JUSTIFY_CENTER, self.OnAlignCenter)
        wx.EVT_UPDATE_UI(self, wx.ID_JUSTIFY_CENTER, self.OnUpdateAlignCenter)
        wx.EVT_TOOL(self, wx.ID_JUSTIFY_RIGHT, self.OnAlignRight)
        wx.EVT_UPDATE_UI(self, wx.ID_JUSTIFY_RIGHT, self.OnUpdateAlignRight)
        wx.EVT_TOOL(self, wx.ID_JUSTIFY_FILL, self.OnAlignJustify)
        wx.EVT_UPDATE_UI(self, wx.ID_JUSTIFY_FILL, self.OnUpdateAlignJustify)
        #wx.EVT_TOOL(self, toolbar_ORDEREDLIST, self.OnNumList)
        #wx.EVT_TOOL(self, toolbar_UNORDEREDLIST, self.OnBulletList)
        wx.EVT_TOOL(self, self.toolbar_ADDCONTENT, self.AddContent)
        wx.EVT_TOOL(self, self.toolbar_UPDATECONTENT, self.UpdateContent)
        tb.Realize()
        
        return tb

    def ForwardEvent(self, evt):
        # The RichTextCtrl can handle menu and update events for undo,
        # redo, cut, copy, paste, delete, and select all, so just
        # forward the event to it.
        self.createContentRichTextCtrl.ProcessEvent(evt)

    def OnStrikethrough(self, evt):
        self.TopLevelParent.DisplayDebug("")
        #self.createContentRichTextCtrl
        
    def OnUpdateBold(self, evt):
        evt.Check(self.createContentRichTextCtrl.IsSelectionBold())

    def OnBold(self, evt):
        self.createContentRichTextCtrl.ApplyBoldToSelection()

    def OnItalic(self, evt): 
        self.createContentRichTextCtrl.ApplyItalicToSelection()
        
    def OnUnderline(self, evt):
        self.createContentRichTextCtrl.ApplyUnderlineToSelection()
        
    def OnAlignLeft(self, evt):
        self.createContentRichTextCtrl.ApplyAlignmentToSelection(wx.richtext.TEXT_ALIGNMENT_LEFT)
        
    def OnAlignRight(self, evt):
        self.createContentRichTextCtrl.ApplyAlignmentToSelection(wx.richtext.TEXT_ALIGNMENT_RIGHT)
        
    def OnAlignCenter(self, evt):
        self.createContentRichTextCtrl.ApplyAlignmentToSelection(wx.richtext.TEXT_ALIGNMENT_CENTRE)
        
    def OnAlignJustify(self, evt):
        self.createContentRichTextCtrl.ApplyAlignmentToSelection(wx.richtext.TEXT_ALIGNMENT_JUSTIFIED)
        
    def OnUpdateItalic(self, evt): 
        evt.Check(self.createContentRichTextCtrl.IsSelectionItalics())
    
    def OnUpdateUnderline(self, evt): 
        evt.Check(self.createContentRichTextCtrl.IsSelectionUnderlined())
    
    def OnUpdateAlignLeft(self, evt):
        evt.Check(self.createContentRichTextCtrl.IsSelectionAligned(wx.richtext.TEXT_ALIGNMENT_LEFT))
        
    def OnUpdateAlignCenter(self, evt):
        evt.Check(self.createContentRichTextCtrl.IsSelectionAligned(wx.richtext.TEXT_ALIGNMENT_CENTRE))
        
    def OnUpdateAlignRight(self, evt):
        evt.Check(self.createContentRichTextCtrl.IsSelectionAligned(wx.richtext.TEXT_ALIGNMENT_RIGHT))
        
    def OnUpdateAlignJustify(self, evt):
        evt.Check(self.createContentRichTextCtrl.IsSelectionAligned(wx.richtext.TEXT_ALIGNMENT_JUSTIFIED))

    # Create Event Function
    def AddContent(self, evt):
        # should be disabled until someone enters content...
        out = StringIO()
        handler = wx.richtext.RichTextXMLHandler()
        txtBuffer = self.createContentRichTextCtrl.GetBuffer()
        handler.SaveStream(txtBuffer, out)
        out.seek(0)
        self.content = out.read()
        out.close()
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("INSERT INTO ResourceTable(resourceData, courseID, resourceTypeID) VALUES(?, ?, 1)", (self.content, self.TopLevelParent.currentCourseID))
        con.commit()
        con.close()
        self.TopLevelParent.currentContentDict = self.TopLevelParent.RefreshListBox(self.listBox, self.TopLevelParent.currentCourseID, 1, None)
        # put code in here to refresh available resources list box...
        # or we could just put code to refresh available resources list box when you select the layout tab button...
        # if that loads too quickly, then do it here
        
    def UpdateContent(self, evt):
        tmpString = self.listBox.GetStringSelection()
        tmpString2 = tmpString.split(".", 1)
        out = StringIO()
        handler = wx.richtext.RichTextXMLHandler()
        txtBuffer = self.createContentRichTextCtrl.GetBuffer()
        handler.SaveStream(txtBuffer, out)
        out.seek(0)
        self.content = out.read()
        out.close()
        con = sqlite3.connect(self.TopLevelParent.database)
        cur = con.cursor()
        cur.execute("UPDATE ResourceTable set resourceData = (?) where resourceID = (?)", (self.content, int(tmpString2[0]),))
        con.commit()
        con.close()
        self.TopLevelParent.currentContentDict = self.TopLevelParent.RefreshListBox(self.listBox, self.TopLevelParent.currentCourseID, 1, None)
        # put code in here to refresh available resources list box...
        # or we could just put code to refresh available resources list box when you select the layout tab button...
        # if that loads too quickly, then do it here
